﻿var resetValue ={};
$(initPage);
function initPage() {
	$("#FILE_RETAIN_DAYS").val($("#hideRetainDays").val());
	$("#LOG_RETAIN_DAYS").val($("#hideLogRetainDays").val());
	$("#DATA_RETAIN_DAYS").val($("#hideDataRetainDays").val());
	$("#RUN_TIME").val($("#hideRunTime").val());
	$("#ETL_LOG_LEVEL").val($("#hideEtlLogLevel").val());
	
	$("#btnEdit").click(function() {
		resetValue.INTERFACE_VER = $("#INTERFACE_VER").val();
		resetValue.SOURCE_FILE_PATH = $("#SOURCE_FILE_PATH").val();
		resetValue.FILE_SAVE_PATH = $("#FILE_SAVE_PATH").val();
		resetValue.FILE_RETAIN_DAYS = $("#FILE_RETAIN_DAYS").val();
		resetValue.LOG_RETAIN_DAYS = $("#LOG_RETAIN_DAYS").val();
		resetValue.DATA_RETAIN_DAYS = $("#DATA_RETAIN_DAYS").val();
		resetValue.RUN_TIME = $("#RUN_TIME").val();
		resetValue.ETL_LOG_LEVEL = $("#ETL_LOG_LEVEL").val();
		resetValue.RUN_STRATEGY = $(".runStrategy[checked]").val();
		resetValue.ACCRUAL_MAN_TYPE = $(".accrualManType[checked]").val();
		resetValue.EXP_FILE_TYPE = $(".expFileType[checked]").val();
		resetValue.RUN_SWITCH = $(".runSwitch[checked]").val();
        resetValue.FILE_ENCRYPT = $(".isEncrypt[checked]").val();
        resetValue.ENCRYPT_PIN = $("#ENCRYPT_PIN").val();
		resetValue.ENCRYPT_KEY_CER = $("#ENCRYPT_KEY_CER").val();
		resetValue.PROP_RDY_FILENAME = $.trim($("#PROP_RDY_FILENAME").val());
		resetValue.PFX_PATH = $("#PFX_PATH").val();

		$("#btnEdit").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled",false);
		$("#INTERFACE_VER,#SOURCE_FILE_PATH,#FILE_SAVE_PATH,#FILE_RETAIN_DAYS,#LOG_RETAIN_DAYS,#DATA_RETAIN_DAYS,#RUN_TIME,#ETL_LOG_LEVEL,#ENCRYPT_PIN,#ENCRYPT_KEY_CER,.runStrategy,.accrualManType,.expFileType,.runSwitch,.isEncrypt,#PROP_RDY_FILENAME_R,#PROP_RDY_FILENAME,#PFX_PATH").attr("disabled",false);
		disableEle(resetValue.RUN_SWITCH);
		disableEncrypt(resetValue.FILE_ENCRYPT);
		disableProp(resetValue.PROP_RDY_FILENAME);
	});
	
	$("#btnSave").click(function() {
		var reg=new RegExp("^[a-zA-Z]:([\\\\/].+)*[\\\\/]?$");
		var sourcePaht = $("#SOURCE_FILE_PATH").val();
		if(!reg.test(sourcePaht)){
			alert("请正确填写文件路径");
			return;
		}
		
		var pfxPath = $("#PFX_PATH").val();
		if(!reg.test(pfxPath)){
			alert("请正确填写文件路径");
			return;
		}
			
		var path = $("#FILE_SAVE_PATH").val();
		if(path.toLowerCase().indexOf("ftp://") != 0) {
			if(!reg.test(path)){
				alert("请正确填写文件路径，或FTP地址（FTP格式如：ftp://user:psw@localhost）");
				return;
			}
		} else {
			//验证FTP
		}
		var confStrs = [];
		confStrs[confStrs.length] = "INTERFACE_VER;" + $("#INTERFACE_VER").val();
		confStrs[confStrs.length] = "SOURCE_FILE_PATH;" + sourcePaht;
		confStrs[confStrs.length] = "FILE_SAVE_PATH;" + path;
		confStrs[confStrs.length] = "FILE_RETAIN_DAYS;" + $("#FILE_RETAIN_DAYS").val();
		confStrs[confStrs.length] = "LOG_RETAIN_DAYS;" + $("#LOG_RETAIN_DAYS").val();
		confStrs[confStrs.length] = "DATA_RETAIN_DAYS;" + $("#DATA_RETAIN_DAYS").val();
		confStrs[confStrs.length] = "RUN_TIME;" + $("#RUN_TIME").val();
		confStrs[confStrs.length] = "ETL_LOG_LEVEL;" + $("#ETL_LOG_LEVEL").val();
		confStrs[confStrs.length] = "RUN_STRATEGY;" + $(".runStrategy[checked]").val();
		confStrs[confStrs.length] = "ACCRUAL_MAN_TYPE;" + $(".accrualManType[checked]").val();
		confStrs[confStrs.length] = "EXP_FILE_TYPE;" + $(".expFileType[checked]").val();
		confStrs[confStrs.length] = "RUN_SWITCH;" + $(".runSwitch[checked]").val();
		confStrs[confStrs.length] = "FILE_ENCRYPT;" + $(".isEncrypt[checked]").val();
	    confStrs[confStrs.length] = "ENCRYPT_PIN;" + $("#ENCRYPT_PIN").val();
		confStrs[confStrs.length] = "ENCRYPT_KEY_CER;" + $("#ENCRYPT_KEY_CER").val();
		var prop = $("#PROP_RDY_FILENAME_R").attr("checked");
		if (prop) {
			if($.validInput("PROP_RDY_FILENAME", "PROP总标志文件名", true, "\<\>\'\"", 50)){
				return false;
			}
		}
		confStrs[confStrs.length] = "PROP_RDY_FILENAME;" + prop + "-" + $.trim($("#PROP_RDY_FILENAME").val());
		confStrs[confStrs.length] = "PFX_PATH;" + pfxPath;
		$.post("m/submit_conf/modify", {
			confStrs:confStrs
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：" + data.msg.text);
				resetValue.INTERFACE_VER = $("#INTERFACE_VER").val();
				resetValue.SOURCE_FILE_PATH = $("#SOURCE_FILE_PATH").val();
				resetValue.FILE_SAVE_PATH = $("#FILE_SAVE_PATH").val();
				resetValue.FILE_RETAIN_DAYS = $("#FILE_RETAIN_DAYS").val();
				resetValue.LOG_RETAIN_DAYS = $("#LOG_RETAIN_DAYS").val();
				resetValue.DATA_RETAIN_DAYS = $("#DATA_RETAIN_DAYS").val();
				resetValue.RUN_TIME = $("#RUN_TIME").val();
				resetValue.ETL_LOG_LEVEL = $("#ETL_LOG_LEVEL").val();
				resetValue.RUN_STRATEGY = $(".runStrategy[checked]").val();
				resetValue.ACCRUAL_MAN_TYPE = $(".accrualManType[checked]").val();
				resetValue.EXP_FILE_TYPE = $(".expFileType[checked]").val();
				resetValue.RUN_SWITCH = $(".runSwitch[checked]").val();
				resetValue.FILE_ENCRYPT = $(".isEncrypt[checked]").val();
				resetValue.ENCRYPT_PIN = $("#ENCRYPT_PIN").val();
				resetValue.ENCRYPT_KEY_CER = $("#ENCRYPT_KEY_CER").val();
				resetValue.PROP_RDY_FILENAME = $.trim($("#PROP_RDY_FILENAME").val());
				resetValue.PFX_PATH = $("#PFX_PATH").val();
				
				$("#btnCancle,#btnSave").attr("disabled", true);
				$("#btnEdit").attr("disabled",false);
				$("#INTERFACE_VER,#SOURCE_FILE_PATH,#FILE_SAVE_PATH,#FILE_RETAIN_DAYS,#LOG_RETAIN_DAYS,#DATA_RETAIN_DAYS,#RUN_TIME,#ETL_LOG_LEVEL,#ENCRYPT_PIN,#ENCRYPT_KEY_CER,.runStrategy,.accrualManType,.expFileType,.runSwitch,.isEncrypt,#PROP_RDY_FILENAME_R,#PROP_RDY_FILENAME,#PFX_PATH").attr("disabled", true);
			} 
		}, "json");
	});
	
	$("#btnCancle").click(function() {
		$("#INTERFACE_VER").val(resetValue.INTERFACE_VER);
		$("#SOURCE_FILE_PATH").val(resetValue.SOURCE_FILE_PATH);
		$("#PFX_PATH").val(resetValue.PFX_PATH);
		$("#FILE_SAVE_PATH").val(resetValue.FILE_SAVE_PATH);
		$("#FILE_RETAIN_DAYS").val(resetValue.FILE_RETAIN_DAYS);
		$("#LOG_RETAIN_DAYS").val(resetValue.LOG_RETAIN_DAYS);
		$("#DATA_RETAIN_DAYS").val(resetValue.DATA_RETAIN_DAYS);
		$("#RUN_TIME").val(resetValue.RUN_TIME);
		$("#ETL_LOG_LEVEL").val(resetValue.ETL_LOG_LEVEL);
		$(".runStrategy").each(function(){
			$(this).attr("checked", this.value == resetValue.RUN_STRATEGY);
		});
		$(".accrualManType").each(function(){
			$(this).attr("checked", this.value == resetValue.ACCRUAL_MAN_TYPE);
		});
		$(".expFileType").each(function(){
			$(this).attr("checked", this.value == resetValue.EXP_FILE_TYPE);
		});
		$(".runSwitch").each(function(){
			$(this).attr("checked", this.value == resetValue.RUN_SWITCH);
		});
		$(".isEncrypt").each(function(){
			$(this).attr("checked", this.value == resetValue.FILE_ENCRYPT);
		});
		$("#ENCRYPT_PIN").val(resetValue.ENCRYPT_PIN);
		$("#ENCRYPT_KEY_CER").val(resetValue.ENCRYPT_KEY_CER);
		$("#PROP_RDY_FILENAME").val(resetValue.PROP_RDY_FILENAME);
		$("#PROP_RDY_FILENAME_R").attr("checked",resetValue.PROP_RDY_FILENAME);
		$("#btnCancle,#btnSave").attr("disabled", true);
		$("#btnEdit").attr("disabled",false);
		$("#INTERFACE_VER,#SOURCE_FILE_PATH,#FILE_SAVE_PATH,#FILE_RETAIN_DAYS,#LOG_RETAIN_DAYS,#DATA_RETAIN_DAYS,#RUN_TIME,#ETL_LOG_LEVEL,#ENCRYPT_PIN,#ENCRYPT_KEY_CER,.runStrategy,.accrualManType,.expFileType,.runSwitch,.isEncrypt,#PROP_RDY_FILENAME_R,#PROP_RDY_FILENAME,#PFX_PATH").attr("disabled", true);
	});
	
	$(".runSwitch").click(function() {
		disableEle(this.value);
	});
	$(".isEncrypt").click(function() {
		disableEncrypt(this.value);
	});
	$("#PROP_RDY_FILENAME_R").click(function() {
		disableProp($(this).attr("checked"));
	});
}

function disableEle(runSwitch) {
	if(runSwitch == 1) {
		$("#RUN_TIME,.runStrategy").attr("disabled",false);
	} else {
		$("#RUN_TIME,.runStrategy").attr("disabled",true);
	}
}

function disableEncrypt(isEncrypt) {
	if(isEncrypt == 1) {
		$("#ENCRYPT_PIN,#ENCRYPT_KEY_CER").attr("disabled",false);
	} else {
		$("#ENCRYPT_PIN,#ENCRYPT_KEY_CER").attr("disabled",true);
	}
}

function disableProp(fileName) {
	if(fileName) {
		$("#PROP_RDY_FILENAME").attr("disabled",false);
	} else {
		$("#PROP_RDY_FILENAME").attr("disabled",true).val("");
	}
}